package com.example.comm.mqtt;

import com.example.comm.pojo.WarnFlag;
import com.example.comm.socket.UDPSend;
import com.example.comm.utils.LightDataUtils;
import com.example.comm.utils.WarnDataUtils;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class PushCallback implements MqttCallback {

    public static Map<String ,String> mp = new HashMap<>() ;

    @Autowired
    private MqttConfiguration mqttConfiguration;

    @Override
    public void connectionLost(Throwable cause) {
        log.info("连接断开，正在重连");
        MqttPushClient mqttPushClient = mqttConfiguration.getMqttPushClient();
        if (null != mqttPushClient) {
            mqttPushClient.connect(mqttConfiguration.getHost(), mqttConfiguration.getClientid(), mqttConfiguration.getUsername(),
                    mqttConfiguration.getPassword(), mqttConfiguration.getTimeout(), mqttConfiguration.getKeepalive());
            log.info("已重连");
        }

    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {

    }

    @Override
    public void messageArrived(String topic, MqttMessage message)   {

        String res = new String(message.getPayload());
        log.info("接收消息主题 : " + topic);
        log.info("接收消息Qos : " + message.getQos());
        log.info("接收消息内容 : " + new String(message.getPayload()));




        try{



            if(topic.equals("test/publish")) {

                //对接收的几种Mmqtt信息进行区分，放入不同的datautil中。有红绿灯数据/小车预警数据/mq3酒精数据/安全带数据
                //分为2类，通过HTTP传输给arkts和通过UDP
                if (res.contains("light"))       //红绿灯
                {
                    LightDataUtils.getInstance().put(topic, new String(message.getPayload()));
                } else if (res.contains("warnFlag"))    //预警信息
                {
                    WarnDataUtils.getInstance().put(topic, new String(message.getPayload()));
                    int fron = res.indexOf("=");
                    //返回给arkts修改UI。
                    WarnFlag warnFlag = WarnFlag.getInstance();
                    warnFlag.setFlag(res.charAt(fron + 1));
                } else {
                    //酒精和安全带
                    //通过UDP协议发给arkts
                    //添加时间数据，方便存储和判断重复
                    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    res += sdf.format(System.currentTimeMillis());
                    System.out.println("酒精数据，后端发送的是"+res);
                    UDPSend udpSend = new UDPSend();
                    udpSend.sendMessage(res);
                }


            } else if(topic.equals("car_data")) {
                //接收另一车发来的预警信息。格式为"warnFlag="+warnUtil.getFlag()
                String flagString = res.substring(res.indexOf("="),res.indexOf("=")+1);
                int flag = Integer.parseInt(flagString);
                WarnFlag warnFlag = WarnFlag.getInstance();
                warnFlag.setFlag(flag);

                //发送给前端arkts
                //形式：前端通过HTTP请求获得数据，通过WarnUtil传递数据，0无事发生，1前方碰撞，2交叉路口，3红绿灯预警


            }











            }catch (Exception e) {
//            e.printStackTrace();
//            log.info("222222222222");

        }


    }

}